#!/bin/bash

# 默认安装，要求能翻墙
default() {
	curl -sfL https://get.k3s.io | sh -s - server \
		--disable-network-policy \
		--disable-cloud-controller \
		--disable-helm-controller \
		--flannel-backend=none \
		--data-dir=/data/k3s \
		--disable=traefik,local-storage,metrics-server
}

# 安装到 devcloud 环境
install_devcloud() {
	./k3s-install.sh -s - server \
		--disable-network-policy \
		--disable-cloud-controller \
		--disable-helm-controller \
		--data-dir=/data/k3s \
		--disable=traefik,local-storage,metrics-server
}

install_tencentcloud_china() {
	INSTALL_K3S_MIRROR=cn ./k3s-install-china.sh -s - server \
		--disable-network-policy \
		--disable-cloud-controller \
		--disable-helm-controller \
		--data-dir=/data/k3s \
		--disable=traefik,local-storage,metrics-server
}

install_tencentcloud() {
	./k3s-install.sh -s - server \
		--disable-network-policy \
		--disable-cloud-controller \
		--disable-helm-controller \
		--data-dir=/data/k3s \
		--disable=traefik,local-storage,metrics-server
}

server() {
	curl -sfL https://get.k3s.io | sh -s - server \
		--disable-cloud-controller \
		--disable-network-policy \
		--disable-helm-controller \
		--data-dir=/data/k3s \
		--disable=traefik,local-storage,metrics-server,servicelb
}

# 国内安装 k3s，不要求能翻墙，适用国内云服务器
china() {
	install_k3s_airgap
	install_cni
	INSTALL_K3S_SKIP_DOWNLOAD=true ./k3s-install.sh -s - server \
		--disable-network-policy \
		--disable-helm-controller \
		--flannel-backend=none \
		--data-dir=/data/k3s \
		--disable=traefik,local-storage,metrics-server
}

client() {
	INSTALL_K3S_MIRROR=cn curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | sh -s - server \
		--kubelet-arg="--hostname-override=10.10.10.2" \
		--disable-kube-proxy \
		--disable-cloud-controller \
		--disable-network-policy \
		--disable-helm-controller \
		--disable=traefik,local-storage,metrics-server,servicelb,coredns
}

# 路由器安装k3s, 指定 hostname，避免用公网 IP，重启后变化导致证书失效，coredns 无法连接 apiserver
install_router() {
	INSTALL_K3S_MIRROR=cn ./k3s-install-china.sh -s - server \
		--kubelet-arg="node-ip=10.10.10.2" \
		--data-dir=/data/k3s \
		--disable-network-policy \
		--disable-helm-controller \
		--disable=traefik,local-storage,metrics-server
}

install_router_old() {
	install_k3s_airgap
	install_cni
	INSTALL_K3S_SKIP_DOWNLOAD=true ./k3s-install.sh -s - server \
		--kubelet-arg="node-ip=10.10.10.2" \
		--data-dir=/data/k3s \
		--disable-network-policy \
		--disable-helm-controller \
		--flannel-backend=none \
		--disable=traefik,local-storage,metrics-server
}

# 更小 k3s 安装脚本
update_script() {
	curl -sfL https://get.k3s.io -o k3s-install.sh
	curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh -o k3s-install-china.sh
	chmod +x k3s-install.sh k3s-install-china.sh
	wget -O system-upgrade-controller.yaml https://github.com/rancher/system-upgrade-controller/releases/latest/download/system-upgrade-controller.yaml
}

# 下载 k3s 离线包
install_k3s_airgap() {
	mkdir -p /var/lib/rancher/k3s/agent/images/
	wget https://ghproxy.com/https://github.com/k3s-io/k3s/releases/latest/download/k3s-airgap-images-amd64.tar -O /var/lib/rancher/k3s/agent/images/k3s-airgap-images-amd64.tar
	wget https://ghproxy.com/https://github.com/k3s-io/k3s/releases/latest/download/k3s -O /usr/local/bin/k3s
	chmod +x /usr/local/bin/k3s
}

# 安装 cni 插件二进制和配置
install_cni() {
	mkdir -p /opt/cni/bin/
	wget https://ghproxy.com/https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz -O /tmp/cni-plugins.tgz
	tar -zxvf /tmp/cni-plugins.tgz -C /opt/cni/bin
	mkdir -p /etc/cni/net.d/
	cp 10-ptp.conflist /etc/cni/net.d/
}

uninstall() {
	/usr/local/bin/k3s-uninstall.sh
}

kubeconfig() {
	mkdir -p ~/.kube
	kubectl config view --raw >~/.kube/config
	echo "please save this into to your local kubeconfig file:"
	echo ""
	kubectl config view --raw
	echo ""
	echo "don't forget change server ip 127.0.0.1 to your public ip address, and remove certificate-authority-data, replace with 'insecure-skip-tls-verify: true'"
}

auto_upgrade() {
	while :; do
		sleep 2
		result=$(kubectl get node | grep master | wc -l)
		if [[ "$result" == "0" ]]; then
			echo "master node is not registered yet, waiting..."
			continue
		fi
		result=$(kubectl get node | grep master | grep NotReady | wc -l)
		if [[ "$result" != "0" ]]; then
			echo "master node not ready, waiting..."
			continue
		else
			echo "k3s is ready, installing system-upgrade-controller..."
			# https://github.com/rancher/system-upgrade-controller/releases/latest/download/system-upgrade-controller.yaml
			kubectl apply -f ./system-upgrade-controller.yaml
			break
		fi
	done

	while :; do
		sleep 2
		result=$(kubectl -n system-upgrade get pod | grep "1/1" | wc -l)
		if [[ "$result" = "0" ]]; then
			echo "system-upgrade-controller is not ready, waiting..."
			continue
		else
			echo "system-upgrade-controller is ready, apply the auto-upgrade plan"
			kubectl apply -f ./auto-upgrade-plan.yaml
			break
		fi
	done
}

enable_bbr() {
	sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf
	sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf
	echo "net.core.default_qdisc = fq" >>/etc/sysctl.conf
	echo "net.ipv4.tcp_congestion_control = bbr" >>/etc/sysctl.conf
	sysctl -p >/dev/null 2>&1
}

install_server() {
	enable_bbr
	server
	auto_upgrade
	kubeconfig
}

install_client() {
	client
	kubeconfig
}

$1
